package gatperdut.chato.core.room;

import gatperdut.chato.constants.CSConstantsMovement.Direction;
import gatperdut.chato.constants.CSConstantsString;
import gatperdut.chato.core.ChatoServer;
import gatperdut.chato.core.characters.BaseController;
import gatperdut.chato.core.item.ItemController;
import gatperdut.chato.core.item.inventory.BoxInventoryController;
import gatperdut.chato.core.item.inventory.InventoryController;
import gatperdut.chato.model.Chato.Room.Room;

public class RoomController {

    private final Room room;

    private final ChatoServer chatoServer;

    private final InventoryController inventoryController;

    private final ExitsController exitsController;

    private final CharactersController charactersController;

    public RoomController( ChatoServer chatoServer, Room room ) {
        this.room = room;
        this.chatoServer = chatoServer;
        this.inventoryController = new BoxInventoryController( getChatoServer(), getRoom().getInventory() );
        this.exitsController = new ExitsController( chatoServer, room );
        this.charactersController = new CharactersController();
    }

    public String show( BaseController whom ) {
        StringBuilder sb = new StringBuilder( CSConstantsString.NEWLINE + getRoom().getTitle() + CSConstantsString.NEWLINE );
        sb.append( getExitsController().getExits() );
        sb.append( CSConstantsString.NEWLINE );
        sb.append( CSConstantsString.NEWLINE );
        sb.append( getRoom().getDescription() );
        sb.append( CSConstantsString.NEWLINE );
        for ( BaseController bc : getCharactersController().getBaseControllers() ) {
            if ( bc != whom ) {
                sb.append( bc.getCharacter().getLongDesc() + CSConstantsString.NEWLINE );
            }
        }

        sb.append( getInventoryController().listContentsLongDescription() );

        sb.append( CSConstantsString.NEWLINE + getInventoryController().computeWeight() + " grams here." );

        return sb.toString();
    }

    public void rxShout( BaseController bc, Direction opposite, String speech ) {
        for ( BaseController present : getCharactersController().getBaseControllers() ) {
            present.getEchoes().rxShoutAdjacentRoom( bc, opposite, speech );
        }
    }

    public void rxThrow( ItemController ic, Direction opposite ) {
        for ( BaseController present : getCharactersController().getBaseControllers() ) {
            present.getEchoes().rxThrowAdjacentRoom( ic, opposite );
        }
    }

    public Room getRoom() {
        return room;
    }

    public ChatoServer getChatoServer() {
        return chatoServer;
    }

    public BoxInventoryController getInventoryController() {
        return (BoxInventoryController) inventoryController;
    }

    public ExitsController getExitsController() {
        return exitsController;
    }

    public CharactersController getCharactersController() {
        return charactersController;
    }

}
